﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using System.Data.Common;

namespace SCRUM.Core.DataProviders
{
    /// <summary>
    /// Privider for comunication with the database
    /// </summary>
    public partial class DBDataProvider : DataProvider
    {
        /// <summary>
        /// Creates new type of the widget with specified parameters
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="enabled">if set to <c>true</c> [enabled].</param>
        public override void CreateWidgetType(string name, string description, bool enabled)
        {
            try
            {
                DbParameter dbName = CreateParameter("Name", name);
                DbParameter dbEnabled = CreateParameter("Enabled", enabled);
                connection.Open();


                ExecuteNonQuery("widgetType/CreateWidgetType", dbName, dbEnabled);

            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Saves the type of the widget.
        /// </summary>
        /// <param name="widgetType">Type of the widget.</param>
        public override void SaveWidgetType(WidgetType widgetType)
        {
            try
            {
                DbParameter dbID = CreateParameter("ID", widgetType.ID);
                DbParameter dbName = CreateParameter("Name", widgetType.Folder);
                DbParameter dbEnabled = CreateParameter("Enabled", widgetType.Enabled);

                connection.Open();

                ExecuteNonQuery("widgetType/SaveWidgetType", dbID, dbName, dbEnabled);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Gets the type of the widget from db data reader
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>Widget Type</returns>
        private WidgetType GetWidgetType(DbDataReader data)
        {
            return new WidgetType(
                 GetIntByColumn(data, "ID")
                 ,GetStringByColumn(data, "NAME")
                 ,GetBoolByColumn(data, "ENABLED")
            );
        }

        /// <summary>
        /// Gets all widget types.
        /// </summary>
        /// <returns>List of widget types</returns>
        public override IList<WidgetType> GetAllWidgetTypes()
        {
            IList<WidgetType> widgetTypes = new List<WidgetType>();
            try
            {
                connection.Open();
                DbDataReader data = GetGrid("widgetType/GetAllWidgetsTypes");

                while (data.Read())
                {
                    WidgetType widgetType = GetWidgetType(data);
                    widgetTypes.Add(widgetType);
                }
            }
            finally
            {
                connection.Close();
            }
            return widgetTypes;
        }

        /// <summary>
        /// Gets all available widget types for user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>List of Widget types available for user</returns>
        public override IList<WidgetType> GetAllAvailableWidgetTypes(User user)
        {
            IList<WidgetType> widgetTypes = new List<WidgetType>();
            try
            {
                DbParameter userID = CreateParameter("userID", user.ID);

                connection.Open();
                DbDataReader data = GetGrid("widgetType/GetAllAvailableWidgets", userID);
                
                while (data.Read())
                {
                    WidgetType widgetType = GetWidgetType(data);
                    widgetTypes.Add(widgetType);
                }
            }
            finally
            {
                connection.Close();
            }
            return widgetTypes;
        }

        

        /// <summary>
        /// Gets the widget type by ID.
        /// </summary>
        /// <param name="ID">The ID.</param>
        /// <returns>Widget type identified by ID</returns>
        public override WidgetType GetWidgetTypeByID(int ID)
        {
            try
            {
                connection.Open();
                DbParameter dbWidgetTypeID = CreateParameter("widgetTypeID", ID);
                DbDataReader data = GetGrid("widgetType/GetWidgetTypeByID", dbWidgetTypeID);

                if (data.Read())
                {
                    return GetWidgetType(data);
                }
                else throw new Exceptions.Database.InconsistentDatabaseException(
                    String.Format("Widget with following ID {0} not foud",ID.ToString())
                );
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
